<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class LinkAction extends AdminAction
{

	protected $Link;


	public function _initialize()
	{
		parent::_initialize();
		$this->Link = D('Link');
	}


	//友情链接列表
	public function linkList()
	{
		import('ORG.Util.Page');

		$total = $this->Link->where($where)->count();
		$Page = new Page($total);
		$pagination = $Page->show();
		$linkList = $this->Link->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign('pagination', $pagination);
		$this->assign('linkList', $linkList);
		$this->display();
	}


	//新增链接
	public function addLink()
	{
		$this->editLink();
	}


	//编辑链接
	public function editLink()
	{
		$linkID = isset($_REQUEST['link_id']) ? intval($_REQUEST['link_id']) : 0;
		$linkInfo = $this->Link->find($linkID);

		if ($this->isPost())
		{
			$_validate = array(
        		array('name', 'require', '{%link.link_name_required}'),
        		array('url', 'url', '{%link.link_url_invalid}'),
        		array('logo', 'url', '{%link.link_logo_invalid}', 2),
        	);
        	$this->Link->setProperty("_validate", $_validate);
			if (!$this->Link->create())
			{
				$this->error($this->Link->getError());
			}

			$data = array(
				'name'          => $_POST['name'],
				'status'        => $_POST['status'],
				'url'           => $_POST['url'],
				'description'   => $_POST['description'],
				'logo'          => $_POST['logo'],
				'target'        => $_POST['target'],
				'display_order' => intval($_POST['display_order']),
			);

			if (empty($linkID))
			{
				$lastID = $this->Link->add($data);
			}
			else
			{
				if (empty($linkInfo))
				{
			 		$this->error(L('link.link_not_exist'));
				}
				$where = array('link_id' => $linkID);
				$this->Link->where($where)->save($data);
			}

			$this->Link->cacheLinks();

			$this->assign('jumpUrl', __URL__.'/linkList');
			$this->success(L('link.link_edited_succeed'));
		}
		else
		{
			$linkStatusRadios = array(1 => L('yes'), 0 => L('no'));
			$linkStatusChecked = isset($linkInfo['status']) ? $linkInfo['status'] : 1;
			$this->assign('linkStatusRadios', $linkStatusRadios);
			$this->assign('linkStatusChecked', $linkStatusChecked);

			$linkTargetRadios = array(
				'_blank' => L('_blank'),
				'_self'  => L('_self'),
				'_top'   => L('_top'),
			);
			$linkTargetChecked = isset($linkInfo['target']) ? $linkInfo['target'] : '_blank';
			$this->assign('linkTargetRadios', $linkTargetRadios);
			$this->assign('linkTargetChecked', $linkTargetChecked);

			$this->assign('rs', $linkInfo);
			$this->display('editLink');
		}
	}


	//管理链接
	public function manageLink()
	{
	    if ($this->isPost())
		{
		    if (!empty($_POST['display_order']))
		    {
		     	foreach ($_POST['display_order'] as $key => $value)
		     	{
		     	    $data =array(
		     	        'status' => intval($_POST['status'][$key]),
		     	        'display_order' => intval($_POST['display_order'][$key]),
		     	    );
		     	    $map = array('link_id' => $key);
		     	    $this->Link->where($map)->save($data);
		     	}
		    }

		    if (!empty($_POST['mods']))
		    {
		        $map = array('link_id' => array('in', $_POST['mods']));
		    	$this->Link->where($map)->delete();
		    }

		    $this->Link->cacheLinks();
		    $this->success(L('link.link_updated_succeed'));
		}
	}

}

/* End of file LinkAction.class.php */
/* Location: ./App/Lib/Action/Admin/LinkAction.class.php */
